package com.wanhe.mealbookmodule.fragment;

import android.support.v4.view.ViewPager;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.wanhe.base.BaseFragment;
import com.wanhe.mealbookmodule.R;
import com.wanhe.mealbookmodule.adapter.CommonAdapter;
import com.wanhe.mealbookmodule.adapter.CommonBasePagerAdapter;
import com.wanhe.mealbookmodule.adapter.MealBookPagerAdapter;
import com.wanhe.mealbookmodule.adapter.views.IListViewAdapter;
import com.wanhe.mealbookmodule.adapter.views.OnDataOperationResultListener;
import com.wanhe.mealbookmodule.injector.component.DaggerMealBookComponent;
import com.wanhe.mealbookmodule.injector.module.MealBookModule;
import com.wanhe.mealbookmodule.presenter.MealBookPresenter;
import com.wanhe.mealbookmodule.presenter.views.ILoadingView;
import com.wanhe.mealbookmodule.views.MealBookPageItemView;

import javax.inject.Inject;

/**
 * Created by cdj on 2017/6/22.
 */

public class MealBookFragment extends BaseFragment implements ILoadingView {

	public static final String TAG = "MealBookFragment";

	@Inject
	MealBookPresenter	presenter;
	private ViewPager	pager;

	private int lastPagePosition = -1;

	private OnPageChangedListener	pageChangedListener;
	private MealBookPagerAdapter	pagerAdapter;

	public void setPageChangedListener(OnPageChangedListener pageChangedListener) {
		this.pageChangedListener = pageChangedListener;
	}

	/**
	 * 外部传入的菜品类型数据适配器，
	 * 因为关于菜品类型的布局，每家店都有不同的布局，于是将布局独立于该层之外，
	 * 仅仅需要将适配器传入进来，对适配器的数据进行变更通知，即可变更上层应用布局中显示的内容了
	 *
	 * @author 陈德基
	 * @time 2017/8/1 14:53
	 */
	private CommonAdapter mTypeListAdapter;

	@Override
	protected int attachLayoutRes() {
		return R.layout.coorder_view_layout;
	}

	@Override
	protected void initViews() {

		pager = (ViewPager) mRootView.findViewById(R.id.vp_mealbook_pager);
		pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
			}

			@Override
			public void onPageSelected(int position) {
				if (lastPagePosition >= 0) {
					MealBookPageItemView lastItemView = (MealBookPageItemView) pager
							.getChildAt(lastPagePosition);
					lastItemView.pausePage();
				}
				MealBookPageItemView itemView = (MealBookPageItemView) pager.getChildAt(position);
				itemView.initPageData();
				lastPagePosition = position;
				if (pageChangedListener != null) {
					pageChangedListener.onPageSelected(lastPagePosition, itemView.getTypeID());
				}
			}

			@Override
			public void onPageScrollStateChanged(int state) {
			}
		});
		pagerAdapter = new MealBookPagerAdapter(getContext());
		pagerAdapter.setOnDataOperationResultListener(new OnDataOperationResultListener() {
			@Override
			public void noMoreData() {
				hideLoading();
			}

			@Override
			public void empty() {
				hideLoading();
			}

			@Override
			public void listLoaded(IListViewAdapter adapter) {
				pager.setOffscreenPageLimit(((CommonBasePagerAdapter) adapter).getCount());
			}
		});
		pager.setAdapter(pagerAdapter);

		DaggerMealBookComponent.builder().baseApplicationComponent(getAppComponent())
				.mealBookModule(new MealBookModule(this, pagerAdapter, mTypeListAdapter)).build()
				.inject(this);

		presenter.showMealBookPages(getContext());

	}

	/**
	 * Methor Name:setTypeListAdapter<br/>
	 * Package Name:com.wanhe.mealbookmodule.fragment<br/>
	 * Date:2017/8/1 14:55<br/>
	 * author:chendeji<br/>
	 * desc: 用于传入上层应用关于菜品分类布局的数据适配器<br/>
	 * <h1>！！！！！！特别注意！！！！！！！</h1><br/>
	 * 该方法需要在此Fragment被commit到Fragmentmanager的事务处理之前调用<br/>
	 * 不然不能保证每次都能获取到菜品类型的数据<br/>
	 * <br/>
	 * Copyright (c) 2017/8/1, 781571323@qq.com All Rights Reserved.
	 *
	 * @param adapter
	 *            外部分类导航布局的数据适配器
	 */
	public void setTypeListAdapter(CommonAdapter adapter) {
		this.mTypeListAdapter = adapter;
	}

	/**
	 * Methor Name:jumpToType<br/>
	 * Package Name:com.wanhe.mealbookmodule.fragment<br/>
	 * Date:2017/8/2 14:20<br/>
	 * author:chendeji<br/>
	 * desc: 切换菜单也到指定的分类<br/>
	 * Copyright (c) 2017/8/2, 781571323@qq.com All Rights Reserved.
	 *
	 * @param tid
	 *            分类ID
	 * @return
	 *
	 */
	public void jumpToType(int tid) {
		jumpToTypeFirstPage(tid);
	}

	private void jumpToTypeFirstPage(int tid) {
		int pagePositionWithTypeID = pagerAdapter.getPagePositionWithTypeID(tid);
		pager.setCurrentItem(pagePositionWithTypeID, false);
	}

	public interface OnPageChangedListener {
		void onPageSelected(int position, int tid);
	}

	@Override
	public void onLoading() {
		showLoading();
	}

	@Override
	public void onLoaded() {
		hideLoading();
	}

	@Override
	public void onLoadFail(String msg) {
		Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show();
	}
}
